
/*********************************************
This file is part of HIM.

HIM is a module of BCI++ framowork for the implementation of Brain Computer Interface systems.
HIM is intended for use with BCI++ only.

BCI++ is a project of the Sensibilab of Politecnico Di Milano

HIM is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as 
published by the Free Software Foundation, either version 3 of 
the License, or (at your option) any later version.

HIM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU General Public License
and the GNU Lesser General Public License along with HIM.  
If not, see <http://www.gnu.org/licenses/>.
*************************************************************/

/*!
\file	MatAlgorithm.h

  \author	Luca Maggi luca.maggi@polimi.it
  
	\date	2008-02-09 creation \n
	2008-10-07 last modification
	
	  \b Org: 
	  SENSIBILAB - Politecnico di Milano
	  
	\b COPYRIGHT: 
	LGPL
		
	\class	MatAlgorithm MatAlgorithm.h
		  
	\brief	
	This class manages all operation related to the processing. Processing classed should be Inherited fro this class
*/


#pragma once

#include "MatlabEng.h"
#include "Algorithm.h"


typedef void*	(*hmxGetData)	(const mxArray*);
typedef bool	(*hmxIsDouble)	(const mxArray*);
typedef bool	(*hmxIsSingle)	(const mxArray*);
typedef	double	(*hmxGetScalar)	(const mxArray*);
typedef	void	(*hmxSetPr)		(mxArray*,double*);
typedef void	(*hmxFree)		(void*);
typedef mxArray*(*hmxCreateNumericMatrix)(int,int,mxClassID,int);
typedef double*	(*hmxGetPr)		(const mxArray*);



class MatAlgorithm: public Algorithm
{
public:
	/*! \fn 	MatAlgorithm(void)
	Constructor of the object
	*/
	MatAlgorithm(void);
	
	/*! \fn void Close(void)
	Closes the session
	*/
	void Close(void);
	
	virtual ~MatAlgorithm(void);
	/*!
	Call the CMatlabEng to evaluate a stirng in the matlab workspace
	/param Command Specify the string to execute
	/param EnableOutput diplay the output messag in the log window
	*/
	void			EvalString(wxString Command, bool EnableOutput = true);
	
	CMatlabEng		*pMatlab;

private:
	
	char		Output[1000];
	mxArray		*TempVect;

protected:
	void	PutIntoMatlab(char *VarName,c4mMatrix *pBlock);
	
	/*! \fn void	GetFromMatlab(char *VarName,c4mMatrix *pBlock);
	Utility function for the communication with the matlab engine. Gets a variable into the matlab workspace, returns false if the variable 
	doesn't exist
	*/
	bool	GetFromMatlab(char *VarName,c4mMatrix *pBlock);
	/*!\fn void	SetClassification(int Class);
	This function set the classification value to be sent to the AEnima Module
	*/
private:

	/*! \fn 	OpenMatlab(void)
	Open matlab for realtime processing using matlab engine. Call this function in the contructor of the derived classes if needed
	*/
	void OpenMatlab(void);

	
		/*! \fn void	PutIntoMatlab(char *VarName,c4mMatrix *pBlock);
	Utility function for the communication with the matlab engine. Puts a variable into the matlab workspace
	*/

	HINSTANCE				hinstLib;
	hmxGetData				pmxGetData;
	hmxIsDouble				pmxIsDouble;
	hmxIsSingle				pmxIsSingle;
	hmxGetScalar			pmxGetScalar;
	hmxSetPr				pmxSetPr;
	hmxFree					pmxFree;
	hmxGetPr				pmxGetPr;
	hmxCreateNumericMatrix	pmxCreateNumericMatrix;
};


